Appearance
创建对象实例
本节目标:理解“创建对象实例”的核心思路,并能把它用于实际开发或面试表达。
使用new命令后跟类名,跟构造函数的用法完全一致。但类必须使用new调用,否则会报错,这与构造函数不同;见例2:class Bar {
doStuff() {
console.log('stuff');
}
}
var b = new Bar();
b.doStuff() // "stuff"
例2:
class Foo { constructor() { return Object.create(null); }}
Foo()// TypeError: Class constructor Foo cannot be invoked without 'new'